<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>合同智能审核系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #2c3e50;
            --secondary: #3498db;
            --accent: #e74c3c;
            --success: #27ae60;
            --light-bg: #f8f9fa;
            --dark-bg: #34495e;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .main-container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 50px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: var(--primary);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            margin: 0;
            font-weight: 600;
            font-size: 2.5rem;
        }

        .header p {
            opacity: 0.9;
            margin: 10px 0 0;
        }

        .content {
            padding: 30px;
        }

        .upload-section {
            background: var(--light-bg);
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            border: 2px dashed #dce6f4;
            transition: all 0.3s ease;
        }

        .upload-section:hover {
            border-color: var(--secondary);
            transform: translateY(-2px);
        }

        .file-input {
            position: relative;
            margin-bottom: 20px;
        }

        .file-input input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e8ed;
            border-radius: 10px;
            background: white;
        }

        .checkbox-container {
            display: flex;
            align-items: center;
            margin: 20px 0;
        }

        .checkbox-container input {
            margin-right: 10px;
            width: 18px;
            height: 18px;
        }

        .btn-primary {
            background: var(--secondary);
            border: none;
            padding: 12px 30px;
            border-radius: 10px;
            font-weight: 600;
            transition: all 0.3s ease;
            width: 100%;
        }

        .btn-primary:hover {
            background: #2980b9;
            transform: translateY(-2px);
        }

        .results-section {
            display: none;
            margin-top: 30px;
        }

        .rating-card {
            background: linear-gradient(135deg, var(--secondary) 0%, #2980b9 100%);
            color: white;
            padding: 25px;
            border-radius: 15px;
            text-align: center;
            margin-bottom: 25px;
        }

        .rating-score {
            font-size: 3rem;
            font-weight: 700;
            margin: 10px 0;
        }

        .clause-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
            border-left: 5px solid var(--accent);
            transition: transform 0.3s ease;
        }

        .clause-card:hover {
            transform: translateX(5px);
        }

        .suggestion-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
            border-left: 5px solid var(--success);
        }

        .risk-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 600;
        }

        .risk-high {
            background: #ff4757;
            color: white;
        }

        .risk-medium {
            background: #ffa502;
            color: white;
        }

        .risk-low {
            background: #2ed573;
            color: white;
        }

        .loading-spinner {
            display: none;
            text-align: center;
            padding: 40px;
        }

        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid var(--secondary);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto 20px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .section-title {
            color: var(--primary);
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #e1e8ed;
        }

        .file-upload-label {
            display: block;
            padding: 20px;
            border: 2px dashed #dce6f4;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .file-upload-label:hover {
            border-color: var(--secondary);
            background: rgba(52, 152, 219, 0.05);
        }

        .file-upload-label i {
            font-size: 2rem;
            color: var(--secondary);
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="header">
            <h1><i class="fas fa-file-contract me-2"></i>合同智能审核系统</h1>
            <p>上传您的合同文档，获取专业的合规性分析与改进建议</p>
        </div>

        <div class="content">
            <div class="upload-section">
                <form id="uploadForm" enctype="multipart/form-data">
                    <div class="mb-4">
                        <label class="file-upload-label">
                            <i class="fas fa-cloud-upload-alt"></i>
                            <div>点击选择合同文件或拖拽文件到此处</div>
                            <span class="text-muted">支持 .docx, .pdf, .txt 格式</span>
                            <input type="file" name="contract" accept=".docx,.pdf,.txt" required style="display: none;">
                        </label>
                    </div>

                    <div class="checkbox-container">
                        <input type="checkbox" name="wps_markup" value="true" id="wpsMarkup">
                        <label for="wpsMarkup">在WPS中自动标注修改意见（只支持WORD格式合同）</label>
                    </div>

                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search me-2"></i>上传并分析合同
                    </button>
                </form>
            </div>

            <div class="loading-spinner" id="loading">
                <div class="spinner"></div>
                <p>正在分析合同内容，请稍候...</p>
            </div>

            <div class="results-section" id="results">
                <div class="rating-card">
                    <h3><i class="fas fa-star me-2"></i>合规评分</h3>
                    <div class="rating-score" id="ratingScore">0</div>
                    <p>基于人工智能分析的合同合规性评估</p>
                </div>

                <h3 class="section-title"><i class="fas fa-exclamation-triangle me-2"></i>不合规条款</h3>
                <div id="clauses"></div>

                <h3 class="section-title"><i class="fas fa-lightbulb me-2"></i>改进建议</h3>
                <div id="suggestions"></div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const form = document.getElementById('uploadForm');
            const fileInput = form.querySelector('input[type="file"]');
            const fileUploadLabel = document.querySelector('.file-upload-label');
            const loading = document.getElementById('loading');
            const results = document.getElementById('results');
            const ratingScore = document.getElementById('ratingScore');

            // 文件上传标签点击事件
            fileUploadLabel.addEventListener('click', function() {
                fileInput.click();
            });

            // 文件选择变化事件
            fileInput.addEventListener('change', function() {
                if (this.files.length > 0) {
                    const fileName = this.files[0].name;
                    fileUploadLabel.querySelector('div').textContent = fileName;
                }
            });

            // 拖拽事件处理
            fileUploadLabel.addEventListener('dragover', function(e) {
                e.preventDefault();
                this.style.borderColor = '#3498db';
                this.style.background = 'rgba(52, 152, 219, 0.1)';
            });

            fileUploadLabel.addEventListener('dragleave', function() {
                this.style.borderColor = '#dce6f4';
                this.style.background = '';
            });

            fileUploadLabel.addEventListener('drop', function(e) {
                e.preventDefault();
                this.style.borderColor = '#dce6f4';
                this.style.background = '';

                if (e.dataTransfer.files.length) {
                    fileInput.files = e.dataTransfer.files;
                    const fileName = e.dataTransfer.files[0].name;
                    fileUploadLabel.querySelector('div').textContent = fileName;
                }
            });

            // 表单提交事件
            form.addEventListener('submit', async function(e) {
                e.preventDefault();

                if (!fileInput.files.length) {
                    alert('请选择要上传的合同文件');
                    return;
                }

                const formData = new FormData(this);

                loading.style.display = 'block';
                results.style.display = 'none';
                try {
                    const response = await fetch('/upload', {
                        method: 'POST',
                        body: formData
                    });

                    const data = await response.json();

                    if (data.error) {
                        throw new Error(data.error);
                    }

                    // 显示评分（带动画效果）
                    let score = 0;
                    const targetScore = data.overall_rating;
                    const scoreInterval = setInterval(() => {
                        ratingScore.textContent = score;
                        score++;
                        if (score > targetScore) {
                            clearInterval(scoreInterval);
                            ratingScore.textContent = targetScore;
                        }
                    }, 20);

                    // 显示不合规条款
                    let clausesHtml = '';
                    if (data.non_compliant_clauses && data.non_compliant_clauses.length > 0) {
                data.non_compliant_clauses.forEach(clause => {
                            const riskClass = clause.风险等级 === '高' ? 'risk-high' :
                                            clause.风险等级 === '中' ? 'risk-medium' : 'risk-low';

                    clausesHtml += `
                                <div class="clause-card">
                                    <div class="d-flex justify-content-between align-items-center mb-2">
                                        <h5 class="mb-0">问题条款</h5>
                                        <span class="risk-badge ${riskClass}">${clause.风险等级}风险</span>
                                    </div>
                                    <p><strong>原文:</strong> ${clause.原文}</p>
                                    <p><strong>问题描述:</strong> ${clause.问题描述}</p>
                        </div>`;
                });
                    } else {
                        clausesHtml = `<div class="alert alert-success">恭喜！未发现不合规条款。</div>`;
                    }
                document.getElementById('clauses').innerHTML = clausesHtml;

                    // 显示改进建议
                    let suggestionsHtml = '';
                    if (data.suggestions && data.suggestions.length > 0) {
                        data.suggestions.forEach((suggestion, index) => {
                            suggestionsHtml += `
                                <div class="suggestion-card">
                                    <div class="d-flex align-items-start">
                                        <span class="badge bg-success me-2">${index + 1}</span>
                                        <p class="mb-0">${suggestion}</p>
                                    </div>
                                </div>`;
                });
                    } else {
                        suggestionsHtml = `<div class="alert alert-info">合同条款合规，无需修改建议。</div>`;
                    }
                document.getElementById('suggestions').innerHTML = suggestionsHtml;

                    // 显示结果区域
                results.style.display = 'block';

                    // 平滑滚动到结果区域
                    results.scrollIntoView({ behavior: 'smooth' });

            } catch (error) {
                alert('上传失败: ' + error.message);
                    console.error('Error:', error);
            } finally {
                loading.style.display = 'none';
            }
        });
        });
    </script>
</body>
</html>